package com.xinxin.intelligenceexam.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.hss01248.dialog.StyledDialog;
import com.xinxin.intelligenceexam.R;
import com.xinxin.intelligenceexam.base.MyApplication;
import com.xinxin.intelligenceexam.contract.ExamContract;
import com.xinxin.intelligenceexam.domain.Question;
import com.xinxin.intelligenceexam.fragment.ExamPagerFragment;
import com.xinxin.intelligenceexam.fragment.adapter.ExamPagerFragmentPagerAdapter;
import com.xinxin.intelligenceexam.presenter.ExamPresenterImpl;
import com.xinxin.intelligenceexam.utils.SnackbarUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnPageChange;

public class ExamActivity extends AppCompatActivity implements ExamContract.View {



    @BindView(R.id.pager_exam)
    ViewPager mPagerExam;
    @BindView(R.id.tv_prepage)
    TextView mTvPrepage;
    @BindView(R.id.tv_menu)
    TextView mTvMenu;
    @BindView(R.id.tv_nextpage)
    TextView mTvNextpage;
    ExamPagerFragmentPagerAdapter mExamPagerFragmentPagerAdapter;
    List<Question> mQuestionList = new ArrayList<>();
    List<ExamPagerFragment> mExamPagerFragmentList = new ArrayList<>();

    ExamContract.Presenter mPresenter;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam);
        ButterKnife.bind(this);
        mContext = this;
        mPresenter = new ExamPresenterImpl();
        mPresenter.attachView(this);
        initData();
    }

    @Override
    public void initData() {
        mPresenter.getExamQuestions(MyApplication.DEFAULT_EXAM_QUESTION_NUM);
    }

    @Override
    public void initView() {
        for (int i = 0; i < mQuestionList.size(); i++) {
            ExamPagerFragment examPagerFragment = ExamPagerFragment.newInstance(i + 1, mQuestionList.get(i));
            mExamPagerFragmentList.add(examPagerFragment);
        }
        mExamPagerFragmentPagerAdapter = new ExamPagerFragmentPagerAdapter(getSupportFragmentManager(), mExamPagerFragmentList);
        mPagerExam.setAdapter(mExamPagerFragmentPagerAdapter);
    }

    @Override
    public void snackBarShow(String msg) {
        SnackbarUtils.Short(mPagerExam, msg).danger().show();
    }


    @OnClick({R.id.tv_prepage, R.id.tv_menu, R.id.tv_nextpage})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_prepage:
                break;
            case R.id.tv_menu:
                break;
            case R.id.tv_nextpage:
                break;
        }
    }

    @OnPageChange(R.id.pager_exam)
    void onPageSelected(int position) {

    }

    @Override
    public void startExam(List<Question> questions) {
        mQuestionList = questions;
        initView();
    }

    @Override
    public void showLoading() {
        StyledDialog.buildLoading(mContext, "请稍后，正在拉取试题", false, false);
    }
}
